import 'dart:async';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:private_journey/GlobalInfo.dart';
import 'package:private_journey/States.dart';
import 'package:private_journey/services/KeyboardSerivce.dart';
import 'package:shadcn_flutter/shadcn_flutter.dart' as shadcn;
import 'package:signals/signals_flutter.dart';
import 'package:window_manager/window_manager.dart';

class TimeComponent extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    if (Platform.isWindows) {
      return TimeComponentStateWindows();
    }
    return TimeComponentState();
  }
}

class TimeComponentState extends State<TimeComponent> {
  late Timer timer;

  //自动更新时间，每秒更新一次
  @override
  void initState() {
    super.initState();
    timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      if (mounted) {
        //判断当前窗口是不是已经最小化了，如果最小化了，就不更新时间了
        currentTimeHour.value = DateTime.now().hour;
        currentTimeMinute.value = DateTime.now().minute;
        currentTimeSecond.value = DateTime.now().second;
      }
    });
  }

  @override
  void dispose() {
    timer.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Watch((context) {
      return Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          shadcn.Text(
            '${paddingZero(currentTimeHour.value)}:${paddingZero(currentTimeMinute.value)}',
          ).h1,
          shadcn.Text(' ${paddingZero(currentTimeSecond.value)}').h4,
        ],
      );
    });
  }
}

String paddingZero(int value) {
  return value.toString().padLeft(2, '0');
}

class TimeComponentStateWindows extends State<TimeComponent>
    with WindowListener {
  late Timer timer;
  bool isMinimized = false;

  @override
  void onWindowClose() {
    logger.i("Sending stop signal to isolate...");
    Dio dio = Dio();
    dio.get('http://localhost:8080/close');
    super.onWindowClose();
  }

  //自动更新时间，每秒更新一次
  @override
  void initState() {
    super.initState();
    windowManager.addListener(this);
    timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      if (mounted) {
        //判断当前窗口是不是已经最小化了，如果最小化了，就不更新时间了
        if (!isMinimized) {
          currentTimeHour.value = DateTime.now().hour;
          currentTimeMinute.value = DateTime.now().minute;
          currentTimeSecond.value = DateTime.now().second;
        }
      }
    });
  }

  @override
  void onWindowMinimize() {
    isMinimized = true;
  }

  @override
  void onWindowRestore() {
    isMinimized = false;
  }

  @override
  void dispose() {
    windowManager.removeListener(this);
    timer.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Watch((context) {
      return Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          shadcn.Text(
            '${paddingZero(currentTimeHour.value)}:${paddingZero(currentTimeMinute.value)}',
          ).h1,
          shadcn.Text(' ${paddingZero(currentTimeSecond.value)}').h4,
        ],
      );
    });
  }
}
